package com.opencloud.iot.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.iot.client.model.entity.IotRaspiBind;
import com.opencloud.iot.client.model.entity.IotRaspiBindInfo;
import com.opencloud.iot.client.model.entity.IotRaspiInfo;
import com.opencloud.iot.client.model.entity.IotWatchinfo;
import com.opencloud.iot.provider.mapper.IotRaspiBindInfoMapper;
import com.opencloud.iot.provider.mapper.IotRaspiBindMapper;
import com.opencloud.iot.provider.mapper.IotRaspiInfoMapper;
import com.opencloud.iot.provider.mapper.IotWatchinfoMapper;
import com.opencloud.iot.provider.service.IotWatchinfoService;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author yeyong
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class IotWatchinfoServiceImpl implements IotWatchinfoService {
    @Autowired
    private IotWatchinfoMapper iotWatchinfoMapper;
    @Autowired
    private IotRaspiInfoMapper iotRaspiInfoMapper;
    @Autowired
    private IotRaspiBindMapper iotRaspiBindMapper;
    @Autowired
    private IotRaspiBindInfoMapper iotRaspiBindInfoMapper;

    @Override
    public IPage<IotWatchinfo> findListPage(PageParams pageParams) {
        IotWatchinfo query = pageParams.mapToObject(IotWatchinfo.class);
        QueryWrapper<IotWatchinfo> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(ObjectUtils.isNotEmpty(query.getMacaddress()),IotWatchinfo::getMacaddress,query.getMacaddress())
                .likeRight(ObjectUtils.isNotEmpty(query.getMacaddress()),IotWatchinfo::getMacaddress,query.getMacaddress())
                .orderByDesc(IotWatchinfo::getLasttime);
        return iotWatchinfoMapper.selectPage(new Page(pageParams.getPage(),pageParams.getLimit()),queryWrapper);
    }


    @Override
    public IotRaspiInfo addIotRaspiInfo(IotRaspiInfo entiy)
    {
        iotRaspiInfoMapper.insert(entiy);
        return entiy;
    }


    @Override
    public IotRaspiInfo updateIotRaspiInfo(IotRaspiInfo entiy)
    {
        iotRaspiInfoMapper.updateById(entiy);
        return entiy;
    }

    @Override
    public IotRaspiInfo getIotRaspiInfoById(Integer id)
    {
        return iotRaspiInfoMapper.selectById(id);
    }

    @Override
    public List<IotRaspiInfo> findIotRaspiInfoList(PageParams pageParams) {
        IotRaspiInfo query = pageParams.mapToObject(IotRaspiInfo.class);
        QueryWrapper<IotRaspiInfo> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getId()), IotRaspiInfo::getId,query.getId());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getMac()), IotRaspiInfo::getMac,query.getMac());
        queryWrapper.lambda().like(ObjectUtils.isNotEmpty(query.getDevicename()), IotRaspiInfo::getDevicename,query.getDevicename());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getOnlineStatus()), IotRaspiInfo::getOnlineStatus,query.getOnlineStatus());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getStatus()), IotRaspiInfo::getStatus,query.getStatus());
        queryWrapper.lambda().orderByDesc(IotRaspiInfo::getCreatetime);
        return iotRaspiInfoMapper.selectList(queryWrapper);
    }


    @Override
    public IotRaspiBind addIotRaspiBind(IotRaspiBind entiy)
    {
        iotRaspiBindMapper.insert(entiy);
        return entiy;
    }


    @Override
    public List<IotRaspiBind> getIotRaspiBindByClassId(Integer classid)
    {
        QueryWrapper<IotRaspiBind> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(classid), IotRaspiBind::getCid,classid);
        return iotRaspiBindMapper.selectList(queryWrapper);
    }

    @Override
    public IotRaspiBind deleteIotRaspiBind(IotRaspiBind entiy)
    {
        QueryWrapper<IotRaspiBind> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(entiy.getMac()), IotRaspiBind::getMac,entiy.getMac());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(entiy.getCid()), IotRaspiBind::getCid,entiy.getCid());
        iotRaspiBindMapper.delete(queryWrapper);
        return entiy;
    }

    @Override
    public IotRaspiInfo getIotRaspiInfoByMac(String mac)
    {
        QueryWrapper<IotRaspiInfo> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(mac), IotRaspiInfo::getMac,mac);
        return iotRaspiInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public List<IotRaspiBindInfo> getIotRaspiBindList(String mac,Integer gid,Integer classid)
    {
        return iotRaspiBindInfoMapper.getRaspiBindList(mac,gid,classid);
    }
}
